package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.DeptMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept> implements DeptService {


    //@Autowired
    ////private EmpMapper empMapper;

    @Autowired
    private EmpService empService;

    @Override
    public void delete(Integer id) {
        //1. 判断部门下是否有员工， 如果有， 需要提示错误信息---更改
        long count = empService.count(Wrappers.<Emp>lambdaQuery().eq(Emp::getId,id));
        if(count > 0){
            throw new BusinessException("部门下有员工， 不能删除");
        }

        //2. 删除部门
        //deptMapper.deleteById(id);
        removeById(id);//根据id删除
    }

    //添加部门
    @Override
    public void add(Dept dept) {
        //1. 补全基础属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //2. 调用Mapper接口
        //deptMapper.insert(dept);
        save(dept);//新增一个部门
    }



    @Override
    public void update(Dept dept) {
        //1. 补全属性
        dept.setUpdateTime(LocalDateTime.now());
        //2. 调用Mapper
        //deptMapper.update(dept);
        updateById(dept);
    }
}
